/* eslint-disable @typescript-eslint/no-explicit-any */
import { boot } from 'quasar/wrappers';
import {
  Notify,
  LoadingBar,
  QInput,
  QInputProps,
  QTable,
  QTableProps,
  QBtn,
  QBtnProps,
  QSelect,
  QSelectProps,
  QTree,
  QTreeProps,
  QField,
  QFieldProps,
} from 'quasar';
import { mdiCheckCircle, mdiAlert, mdiClose } from '@quasar/extras/mdi-v7';

// "async" is optional;
// more info on params: https://v2.quasar.dev/quasar-cli/boot-files
export default boot(async (/* { app, router, ... } */) => {
  // Notify设置默认值和自定义样式
  Notify.setDefaults({
    position: 'top',
    timeout: 4000,
    actions: [{ icon: mdiClose, dense: true, round: true, color: 'white', size: 'sm' }],
  });

  Notify.registerType('success', { icon: mdiCheckCircle, color: 'positive' });
  Notify.registerType('error', { icon: mdiAlert, color: 'negative' });

  // 配置加载条
  LoadingBar.setDefaults({ color: 'red', size: '2px', position: 'top' });

  // 设置各组件的默认属性
  //https://github.com/quasarframework/quasar/issues/7379
  SetComponentDefaults<QBtnProps>(QBtn, { dense: true, noWrap: true });
  SetComponentDefaults<QFieldProps>(QField, { outlined: true, noErrorIcon: true, lazyRules: true });
  SetComponentDefaults<QInputProps>(QInput, { outlined: true, noErrorIcon: true, lazyRules: true });
  SetComponentDefaults<QSelectProps>(QSelect, { outlined: true, noErrorIcon: true });
  SetComponentDefaults<QTreeProps>(QTree, { nodeKey: 'value' });
  SetComponentDefaults<QTableProps>(QTable, { separator: 'cell', wrapCells: true, rowsPerPageOptions: [10, 15, 50, 100, 200] });
});

/**
 * Set some default properties on a component
 */
function SetComponentDefaults<T>(component: any, defaults: Partial<T>): void {
  Object.keys(defaults).forEach((prop: string) => {
    component.props[prop] =
      Array.isArray(component.props[prop]) === true || typeof component.props[prop] === 'function'
        ? { type: component.props[prop], default: (defaults as Record<string, any>)[prop] }
        : { ...component.props[prop], default: (defaults as Record<string, any>)[prop] };
  });
}
